#include "bookinformation.h"
#include "ui_bookinformation.h"
#include "QMessageBox"
bookInformation::bookInformation(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::bookInformation),
    add_dialog(nullptr),
    mod_dialog(nullptr)
{
    ui->setupUi(this);
    ui->tableWidget->setEditTriggers(QAbstractItemView::NoEditTriggers);

    QVector<QStringList> stringlist = fileWriteAndRead().readAndParseFile("book.txt");
//    qDebug()<<stringlist;
    for(int i = 0; i < stringlist.size();i++)
    {
        Book bok(stringlist[i]);
        this->book.insert(bok.book_number,bok);
    }

//    qDebug()<<student.size();
    for(int i = 0; i < BOOK_SIZE;i++)
    {
        ui->tableWidget->insertColumn(i);
    }
//    Book bok("2031053231", "589468528","活着","张三",50.9,9,QDate::currentDate(),"北京市中南海");
//    this->book.insert(bok.book_number,bok);
    printfTable();
}

bookInformation::~bookInformation()
{

    delete ui;
}

void bookInformation::printfTable()
{
    while(ui->tableWidget->rowCount()>0)
    {
        ui->tableWidget->removeRow(ui->tableWidget->rowCount()-1);
    }

    for(int i = 0; i < book.size();i++)
    {
        ui->tableWidget->insertRow(i);
    }

    ui->tableWidget->setHorizontalHeaderItem(0, new QTableWidgetItem("登记号"));
    ui->tableWidget->setHorizontalHeaderItem(1, new QTableWidgetItem("ISBN"));
    ui->tableWidget->setHorizontalHeaderItem(2, new QTableWidgetItem("书名"));
    ui->tableWidget->setHorizontalHeaderItem(3, new QTableWidgetItem("作者"));
    ui->tableWidget->setHorizontalHeaderItem(4, new QTableWidgetItem("单价"));
    ui->tableWidget->setHorizontalHeaderItem(5, new QTableWidgetItem("库存数量"));
    ui->tableWidget->setHorizontalHeaderItem(6, new QTableWidgetItem("出版时间"));
    ui->tableWidget->setHorizontalHeaderItem(7, new QTableWidgetItem("出版社"));

    int i = 0;
    for(auto j = book.begin(); j!=book.end();j++,i++)
    {
        ui->tableWidget->setItem(i, 0, new QTableWidgetItem(j.value().book_number));
        ui->tableWidget->setItem(i, 1, new QTableWidgetItem(j.value().book_ISBN));
        ui->tableWidget->setItem(i, 2, new QTableWidgetItem(j.value().book_name));
        ui->tableWidget->setItem(i, 3, new QTableWidgetItem(j.value().book_writer));
        ui->tableWidget->setItem(i, 4, new QTableWidgetItem(QString::number(j.value().book_price)));
        ui->tableWidget->setItem(i, 5, new QTableWidgetItem(QString::number(j.value().book_num)));
        ui->tableWidget->setItem(i, 6, new QTableWidgetItem(j.value().book_PublishTime.toString("yyyy年MM月")));
        ui->tableWidget->setItem(i, 7, new QTableWidgetItem(j.value().book_PublishUnit));
    }
}

void bookInformation::closeEvent(QCloseEvent *event)
{
    this->close();
    emit is_close();
}

void bookInformation::on_pushButton_add_clicked()
{
    if(add_dialog==nullptr)
    {
        add_dialog = new book_add_Dialog(this);
    }

    add_dialog->exec();

    if(add_dialog->flag == true)
    {
        QMessageBox* message = new QMessageBox(this);
        QStringList cnt = add_dialog->book;
        if(book.find(cnt[0]) == book.end())
        {
            Book bok(cnt[0],cnt[1],cnt[2],cnt[3],cnt[4].toFloat(),cnt[5].toUInt(),QDate::fromString(
                        cnt[6],"yyyy年MM月"),cnt[7]);
            book.insert(bok.book_number,bok);
            message->setText("添加图书成功!");
        }
        else
        {
            message->setText("插入失败!图书登记号重复,请使用修改功能!");
        }
        message->exec();
    }

    printfTable();
    delete add_dialog;
    add_dialog=nullptr;
}

void bookInformation::on_pushButton_mod_clicked()
{
    if(mod_dialog == nullptr)
    {
        mod_dialog = new book_mod_Dialog(this);
    }
//    qDebug()<<add_dialog->flag;

//    add_dialog->show();
//    qDebug()<<add_dialog->flag;
    int row = ui->tableWidget->currentRow();
//    qDebug()<<row;
    if(row != -1)
    {
//        qDebug()<<ui->tableWidget->item(row,0)->text();
        mod_dialog->book=book[ui->tableWidget->item(row,0)->text()].getBook();

        QStringList cnt = mod_dialog->book;
//        qDebug()<<mod_dialog->book;
        mod_dialog->printfData();
//        mod_dialog->book.clear();
        mod_dialog->exec();

        if(mod_dialog->flag == true)
        {

            QMessageBox* message = new QMessageBox(this);
            if(cnt[0] != mod_dialog->book[0])
            {
                if(book.find(mod_dialog->book[0]) == book.end())
                {
                    book.remove(cnt[0]);
                    book.insert(mod_dialog->book[0],Book(mod_dialog->book[0],mod_dialog->book[1],mod_dialog->book[2],
                            mod_dialog->book[3],mod_dialog->book[4].toFloat(),mod_dialog->book[5].toInt(),QDate::fromString( mod_dialog->book[6],"yyyy年MM月"),
                            mod_dialog->book[7]));
                    message->setText("修改图书成功!");
                }
                else
                {
                    message->setText("修改失败!图书登记号重复,请先修改重复项!");
                }

            }
            else
            {
                book.insert(mod_dialog->book[0],Book(mod_dialog->book[0],mod_dialog->book[1],mod_dialog->book[2],
                        mod_dialog->book[3],mod_dialog->book[4].toFloat(),mod_dialog->book[5].toInt(),QDate::fromString( mod_dialog->book[6],"yyyy年MM月"),
                        mod_dialog->book[7]));
                 message->setText("修改图书成功!");
            }
             message->exec();
        }
    }

    printfTable();
    delete  mod_dialog;
    mod_dialog=nullptr;
}

void bookInformation::on_pushButton_delet_clicked()
{
    int row = ui->tableWidget->currentRow();
//    qDebug()<<row;
    if(row != -1)
    {
//        qDebug()<<ui->tableWidget->item(row,0)->text();
        book.remove(ui->tableWidget->item(row,0)->text());

        ui->tableWidget->removeRow(row);
    }

    printfTable();
}

void bookInformation::on_pushButton_save_clicked()
{
    QVector<QStringList> cnt;
    for(Book& bok :this->book)
    {
        cnt.push_back(bok.getBook());
    }
//    qDebug()<<cnt<<endl;
    fileWriteAndRead().writeDataToFile("book.txt",cnt);

}

void bookInformation::on_pushButton_close_clicked()
{
    this->close();
}
